<?php
$JO["imgcodeinvalid"]= JO( array( "EN" => "Image Code invalid.<br/>","VI"=>"Mã Xác Nhận Không Đúng.<br/>") );
$JO["titlemsg"]= JO( array( "EN" => "Title is not empty.<br/>","VI"=>"Tiêu Đề Không Được Để Trống.<br/>") );
$JO["biefcontentmsg"]= JO( array( "EN" => "Biefcontent is not empty.<br/>","VI"=>"Mô Tả Ngắn Gọn Không Được Để Trống.<br/>") );
$JO["contentmsg"]= JO( array( "EN" => "Content is not empty.<br/>","VI"=>"Nội Dung Không Được Để Trống.<br/>") );
$JO["fail"]= JO( array( "EN" => "Fail.<br/>","VI"=>"Đăng Bài Viết Thất bại.<br/>") );
$JO["notscriptmsg"]= JO( array( "EN" => "No script.<br/>","VI"=>"Không Được Sử Dụng SCRIPT. Hãy Kiểm Tra Lại Bài Viết<br/>") );
$JO["success"]= JO( array( "EN" => "success.<br/>","VI"=>"Đăng Bài Viết Thành Công.<br/>") );

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class forumModule{
    public  static function getTopicsOfComment(){
        $SQLCommand="
        select topic.* ,
                (select COUNT(article.topic) from article where article.topic =topic.`key` and article.approval=1 and article.approval=1)  as topicnum,
                (select COUNT(`comment`.article) from `comment`,article where article.`key` =`comment`.article and article.topic=topic.`key`)  as commentnum,
                (select article.`key` from article where article.`topic` =topic.`key` and article.`delete` is null and article.approval=1 order by article.`insert` desc  limit 0,1)  as articlekey,
                (select article.title from article where article.`topic` =topic.`key` and article.`delete` is null and article.approval=1 order by article.`insert` desc limit 0,1)  as articletitle,
                (select article.`approval_date` from article where article.`topic` =topic.`key` and article.`delete` is null and article.approval=1 order by article.`insert` desc limit 0,1)  as articleinsert
        from topic

        where topic.`delete` is null and topic.parent=3
        order by topic.position
        ";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public  static function getPosts($_chude,$_p,$_n){
        $SQLCommand="
        select article.* ,topic.`subject`,`account`.`name`,
                (select COUNT(`comment`.article) from `comment` where comment.`delete` is null and article.`key` =`comment`.article and comment.approval=1)  as commentnum,
                (select comment.`content` from comment where article.`key` =comment.`article` and comment.`delete` is null and comment.approval=1 order by comment.`insert` desc limit 0,1)  as lastcomment,
                (select comment.`approval_date` from comment where article.`key` =comment.`article` and comment.`delete` is null and comment.approval=1 order by comment.`insert` desc limit 0,1)  as lastcommentinsert,
                (select `account`.`name` from `account`,`comment` where article.`key` =`comment`.`article` and `comment`.`delete` is null and `comment`.approval=1
									and account.`key`=`comment`.account	order by `comment`.`insert` desc limit 0,1)  as lastcommentaccountname
        from article,topic,`account`

        where article.`delete` is null
            and topic.`key`=article.topic and topic.parent=3 and article.approval=1
            and article.important is null
            and `account`.`key`=article.`account`
            ".((isset($_chude)||$_chude!="")?("  and topic.`subject`='".$_chude."'"):"")."
        order by  article.important desc,article.`insert` desc
        limit ".($_p-1)*$_n.",$_n
        ";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public  static function getnumPosts($_chude,$_n){
        $SQLCommand="
        select count(*) as numrow
        from article,topic,`account`
        where article.`delete` is null
            and topic.`key`=article.topic and topic.parent=3 and article.approval=1
            and article.important is null
            and `account`.`key`=article.`account`
            ".((isset($_chude)||$_chude!="")?("  and topic.`subject`='".$_chude."'"):"")."

        ";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null){
            $row=$data->fetch_array();
            return ($row["numrow"]%$_n==0)?(int)($row["numrow"]/$_n):((int)($row["numrow"]/$_n)+1);
        }
        return 0;
    }
    public  static function getPost($_key){
        $SQLCommand="
        select article.* ,topic.`subject`,`account`.`name`,`account`.`image` as avatar
        from article,topic,`account`
        where article.`delete` is null
            and topic.`key`=article.topic and topic.parent=3 and article.approval=1
            and `account`.`key`=article.`account`
            and article.`key`=$_key
        ";
        $data=DataProvider::executeSQL($SQLCommand);

        if($data!=null){
            $row=$data->fetch_array();
                return $row;
        }   return null;
        
    }
    public  static function getPosts_important($_chude,$_num){
        $SQLCommand="
        select article.* ,topic.`subject`,`account`.`name`,
                (select COUNT(`comment`.article) from `comment` where comment.`delete` is null and article.`key` =`comment`.article and comment.approval=1)  as commentnum,
                (select comment.`content` from comment where article.`key` =comment.`article` and comment.`delete` is null and comment.approval=1 order by comment.`insert` desc limit 0,1)  as lastcomment,
                (select comment.`approval_date` from comment where article.`key` =comment.`article` and comment.`delete` is null and comment.approval=1 order by comment.`insert` desc limit 0,1)  as lastcommentinsert,
                (select `account`.`name` from `account`,`comment` where article.`key` =`comment`.`article` and `comment`.`delete` is null and `comment`.approval=1
									and account.`key`=`comment`.account	order by `comment`.`insert` desc limit 0,1)  as lastcommentaccountname
        from article,topic,`account`

        where article.`delete` is null
            and topic.`key`=article.topic and topic.parent=3 and article.approval=1
            and article.important=1
            and `account`.`key`=article.`account`
            ".((isset($_chude)||$_chude!="")?("  and topic.`subject`='".$_chude."'"):"")."
        order by  article.important desc,article.`insert` desc
        limit $_num
        ";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static  function addnewPost($_chude,$_tieude,$_noidungnho,$_noidunglon,$_account){
        $SQLCommand="insert into article(`topic`,`title`,`content`,`description`,`account`,`insert`)
                    values($_chude,'$_tieude','$_noidungnho','$_noidunglon',$_account,now())
        ";
        $n=DataProvider::executenoneSQL($SQLCommand);

        if($n==1)
                return true;
        return false;
    }
}


if(isset($_REQUEST["JO_action"])){
    switch ($_REQUEST["JO_action"])
    {
        case "addnewpost":
            if(isset($_SESSION[ "JO_account_id"])){
            $nresult="";
            $serrormsg="";
            $_noidunglon=isset($_REQUEST["txtnoidunglon"])?$_REQUEST["txtnoidunglon"]:"";
            $_noidungnho=isset($_REQUEST["txtnoidungnho"])?$_REQUEST["txtnoidungnho"]:"";
            $_tieude=isset($_REQUEST["txttieude"])?$_REQUEST["txttieude"]:"";
            $_chude=isset($_REQUEST["cbxchude"])?$_REQUEST["cbxchude"]:"";

            $valid = ($_SESSION["security_code"]==$_REQUEST['txtmaxacnhan']?true:false);
            if($valid == false) {
                $nresult="-1";
                $serrormsg=$JO["imgcodeinvalid"];
            }
            if($_noidunglon==""){
                $serrormsg.=$JO["contentmsg"];
                $nresult="-2";
            }
            if($_noidungnho==""){
                $serrormsg.=$JO["biefcontentmsg"];
                $nresult="-3";
            }
            if($_tieude==""){
                $serrormsg.=$JO["titlemsg"];
                $nresult="-4";
            }
            $vowels=array("\r","\t","\n");
            if(     stripos(str_replace($vowels, "", $_noidunglon),"<script")!==false ||
                    stripos(str_replace($vowels, "", $_noidungnho),"<script")!==false ||
                    stripos(str_replace($vowels, "", $_tieude),"<script")!==false
                    ){
                $serrormsg.=$JO["notscriptmsg"];
                $nresult="-21";
            }
            //$_noidunglon=str_replace("script", "&nbsp;script", $_noidunglon);
            if($nresult==""){
                $_tieude=htmlentities($_tieude, ENT_QUOTES,'utf-8');
                $_noidungnho=htmlentities($_noidungnho, ENT_QUOTES,'utf-8');
                $_noidunglon=htmlentities($_noidunglon, ENT_QUOTES,'utf-8');
                //echo($_noidunglon.$_noidunglon);
                //exit;
                $roke=forumModule::addnewPost($_chude,$_tieude,$_noidungnho,$_noidunglon,$_SESSION[ "JO_account_id"]);
                if($roke){
                    $serrormsg.=$JO["success"];
                    $nresult="1";
                }else{
                    $serrormsg.=$JO["fail"];
                    $nresult="0";
                }
            }
            }else{
                $serrormsg="Please Login";
                $nresult="0";
            }
            $data = array('nresult' => $nresult
                            ,'serrormsg' => $serrormsg
                            );
            echo json_encode($data);
            break;
    }
}
?>
